package com.snail.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.snail.cloud.apis.PayFeignApi;
import com.snail.cloud.entities.PayDTO;
import com.snail.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器，订单的增删改查
 *
 * @author snail
 * @date 2024/9/24
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;


    /**
     * 添加订单
     *
     * @param payDTO 支付dto
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @PostMapping("/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        return payFeignApi.addPay(payDTO);
    }

    /**
     * 删除订单
     *
     * @param id ID
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @DeleteMapping("feign/pay/del/{id}")
    public ResultData delOrder(@PathVariable("id") Integer id) {

        ResultData resultData = null;
        try {
            System.out.println("开始调用：" + DateUtil.now());
            resultData = payFeignApi.delPay(id);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("开始调用：" + DateUtil.now());
        }
        return resultData;
    }

    /**
     * 更新订单
     *
     * @param payDTO 支付dto
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @PostMapping("feign/pay/update")
    public ResultData updateOrder(@RequestBody PayDTO payDTO) {
        return payFeignApi.updatePay(payDTO);
    }


    /**
     * 获取订单信息
     *
     * @param id ID
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @GetMapping("feign/pay/get/{id}")
    public ResultData getOrder(@PathVariable("id") Integer id) {
        return payFeignApi.getPayById(id);
    }

    /**
     * 获得所有订单信息
     *
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @GetMapping("feign/pay/getAll")
    public ResultData getAllOrder() {
        return payFeignApi.getPayList();
    }


    /**
     * 通过consul获取配置信息
     *
     * @return {@link String }
     * @author Snail
     * @date 2024/09/25
     */
    @GetMapping("feign/info")
    public ResultData<String> getInfoByConsul() {
        return ResultData.success(payFeignApi.getInfo());

    }
}
